﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    bestlifeDataContext database = new bestlifeDataContext();

    Tbl_Product product = new Tbl_Product();

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            product.Pro_ID = TextBoxProductID.Text;
            product.Code = productcode.SelectedValue;
            product.Pro_Name = TextBoxProductname.Text;
            product.Pro_Price = (decimal)(double.Parse(TextBoxproductprice.Text));
            product.Point_Value = (decimal)(double.Parse(TextBoxpv.Text));
            product.Pro_Type = TextBoxproducttype.Text;
            product.Description = TextBoxdescription.Text;
            product.Cat_ID = DropDownList1.SelectedValue;
            product.Business_Value =(decimal)(double.Parse(TextBoxbv.Text));

             //save file in folder

            if (FileUpload1.PostedFile.ContentType.ToLower().StartsWith("image") && FileUpload1.HasFile)
            {
                string savelocation = Server.MapPath("upload/");
                string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
                //creating filename to avoid file name conflicts.
                string fileName = Guid.NewGuid().ToString();
                //saving file in savedImage folder.
                string savePath = savelocation + fileName + fileExtention;
                FileUpload1.SaveAs(savePath);
                product.Pro_Image = fileName + fileExtention;

            }

            if (FileUpload2.PostedFile.ContentType.ToLower().StartsWith("image") && FileUpload2.HasFile)
            {
                string savelocation = Server.MapPath("upload/");
                string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
                //creating filename to avoid file name conflicts.
                string fileName = Guid.NewGuid().ToString();
                //saving file in savedImage folder.
                string savePath = savelocation + fileName + fileExtention;
                FileUpload2.SaveAs(savePath);
                product.Pro_Image2 = fileName + fileExtention;

            }
            if (FileUpload3.PostedFile.ContentType.ToLower().StartsWith("image") && FileUpload3.HasFile)
            {
                string savelocation = Server.MapPath("upload/");
                string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
                //creating filename to avoid file name conflicts.
                string fileName = Guid.NewGuid().ToString();
                //saving file in savedImage folder.
                string savePath = savelocation + fileName + fileExtention;
                FileUpload3.SaveAs(savePath);
                product.Pro_Image3 = fileName + fileExtention;

            }
            
          


            database.Tbl_Products.InsertOnSubmit(product);
            database.SubmitChanges();
            errormsg.Text = "Product Added Successfully!";
        }
        catch
        {
            errormsg.Text = "Product Cannot Add ";
        }
    }

   



    protected void category_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBoxProductID.Text = "";
        TextBoxProductname.Text = "";
        TextBoxproductprice.Text = "";
        TextBoxpv.Text = "";
        TextBoxproducttype.Text = "";
        TextBoxbv.Text = "";
        TextBoxdescription.Text = "";
        
    }
}
